<?php
class AdminsAction extends AdminAction{
	//管理员列表


	public function index(){
		$admin=D('Admin');
		$list=$admin->relation(true)->select();

		$j=count($list);
		for($i=0;$i<$j;$i++){
			$list[$i]['time']=(!empty($list[$i]['time']))?(date('Y-m-d H:i:s',$list[$i]['time'])):'未登陆';
			$list[$i]['login_ip']=($list[$i]['time'])?$list[$i]['time']:'未登陆';
			$list[$i]['statusname']=$list[$i]['status']==1?'启用':'禁用';
			$list[$i]['groupname']=(!empty($list[$i]['role'][0]['name']))?$list[$i]['role'][0]['name']:'待分组';
			unset($list[$i]['role']);
		}
		//dump($list);
		$this->assign('list',$list);
		$this->display();
	}


	//状态变更
	public function status(){
		$admin=M('Admin');
		if($_GET['id']==1){$this->error('超级管理员状态禁止修改');}
		$data['id']=$_GET['id'];
		$data['status']=$_GET['status']==1?0:1;
		$res=$admin->save($data);
		if($res){$this->success('状态已变更');}else{$this->error('状态变更失败');}
	}

	//添加管理员
	public function add(){
		$this->display();
	}
	public function adding(){

		$admin=D('Admin');

		if(!$admin->create()){
			$this->error($admin->getError());
		}else{
			$data['status']=1;
			$data['username']=trim($_POST['username']);
			$data['password']=md5(trim($_POST['password']));
			$res=$admin->add($data);
			if($res){$this->success('添加成功','index');}else{$this->error('添加失败！');};		
		}

	}

	//分配组
	public function edit(){
		$role=M('Role');
		$groups=$role->getField('id,name');
		$this->groups=$groups;
		$this->id=$_GET['id'];
		$this->username=$_GET['username'];
		$this->display();
	}
	public function editing(){
		$role_user=M('RoleUser');

		$res=$role_user->where('user_id='.$_POST['user_id'])->find();
		//如果存已分配权限则修改，否则添加
		if($res){
			$res=$role_user->where('user_id='.$_POST['user_id'])->save($_POST);
			if($res){$this->success('分配成功','index');}else{$this->error('分配失败1');}
		}else{
			$res=$role_user->add($_POST);
			if($res){$this->success('分配成功','index');}else{$this->error('分配失败');}
		}

	}


}